package mmc.networking;

import mmc.Client;
import mmc.Constants;
import mmc.RemoteServer;
import mmc.server.RemoteClient;

@SuppressWarnings("serial")
public class TextMessage extends Message{

	public final String from;
	public final String msg;
	public final String target; //broadcast if null
	
	public TextMessage(String from, String msg, String target){
		this.from = from;
		this.msg = msg;
		this.target = target;
	}
	
	@Override
	public void executeOnClient(RemoteServer context) {
		if(Client.client.getScene()!=null){
			for(String line : this.toString().split("\n"))
				Client.client.getScene().notifyMsgArrived(line);
		}else
			System.out.println(toString());
	}

	@Override
	public void executeOnServer(RemoteClient context) {
		System.out.println(this);
		context.getServer().broadcast(this);
	}
	
	public String toString(){
		if(Constants.IS_SERVER)
			return (target==null?from:"/w from "+from+" to "+target)+": "+msg;
		else{
			if(target!=null&&Client.client.getScene().player.getRemotePlayer().getName().equalsIgnoreCase(from)&&!Client.client.getScene().player.getRemotePlayer().getName().equalsIgnoreCase(target))
				return "/w to "+target+": "+msg;
			else
				return (target==null?"":"/w from ")+from+": "+msg;
		}
	}
	
}
